package com.yisoo.rs.rsfile.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yisoo.rs.common.utils.PageUtils;
import com.yisoo.rs.common.utils.Query;
import com.yisoo.rs.rsfile.entity.DictEntity;
import com.yisoo.rs.rsfile.service.DictService;
import com.yisoo.rs.rsfile.vo.MovieSourceJsonVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.yisoo.rs.rsfile.dao.BakDao;
import com.yisoo.rs.rsfile.entity.BakEntity;
import com.yisoo.rs.rsfile.service.BakService;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;


@Service("bakService")
public class BakServiceImpl extends ServiceImpl<BakDao, BakEntity> implements BakService {

    @Autowired
    BakDao bakDao;

    @Autowired
    DictService dictService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BakEntity> page = this.page(
                new Query<BakEntity>().getPage(params),
                new QueryWrapper<BakEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageDelContent(Map<String, Object> params) {
        IPage<BakEntity> page = this.page(
                new Query<BakEntity>().getPage(params),
                new QueryWrapper<BakEntity>().select("file_id","file_name","file_create_time","file_import")
        );
        return new PageUtils(page);
    }

    @Override
    public BakEntity getByIdDelContent(Long fileId) {
        QueryWrapper<BakEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("file_id",fileId);
        List<BakEntity> bakEntities = bakDao.selectAllAndDelContent(queryWrapper);
        return bakEntities.get(0);
    }

    @Override
    public boolean importFileById(Long fileId) {
        BakEntity bakEntity = this.getById(fileId);
        byte[] fileContent = bakEntity.getFileContent();
        String content = new String(fileContent);
//        content是原始json串
        JSONObject json = JSON.parseObject(content);
        JSONArray data = json.getJSONArray("data");
//        String jsonString = JSONObject.toJSONString(data);//将array数组转换成字符串
//        System.out.println(jsonString);
        List<MovieSourceJsonVo> dataList = JSON.parseObject(data.toJSONString(), new TypeReference<List<MovieSourceJsonVo>>() {
        });
        DictEntity temp = null;
        List<DictEntity> dictEntityList = new ArrayList<>();
        for (MovieSourceJsonVo m : dataList) {
            temp = new DictEntity(null,fileId,JSON.toJSONString(m),0);
            dictEntityList.add(temp);
        }
//        System.out.println(dictEntityList.size());
//        System.out.println(dictEntityList.get(0).toString());
        return dictService.saveBatch(dictEntityList);
    }


}